package com.iplant.mes.service.po.bcm;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.po.DBBasePO;
import com.iplant.base.utils.annotation.condition.DBCondition;
import com.iplant.base.utils.annotation.field.DBField;
import com.iplant.base.utils.annotation.join.DBJoin;
import com.iplant.mes.service.mesenum.bcm.BCMRuleItemTypes;
import com.iplant.pdm.service.basicenum.pdm.PDMComponentTypes;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@DBBasePO(TableName = "bcm_rule", Schemas = DBSchemas.MES,
        CheckName = {"Code", "ComponentType,ProductTypeID,Name"}, CodeAuto = true, CodePrefix = "BCMR-")
public class BCMRule extends BasePO {

    @DBCondition
    @DBField
    public int ComponentType = 0;

    public String ComponentTypeText = "";

    /**
     * 产品类型ID
     */


    @DBCondition
    @DBField
    public int ProductTypeID = 0;
    /**
     * 产品类型名称
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Name", tableName = "fpc_producttype", tableAsName = "p1", Schemas = DBSchemas.PDM,
            MainTableField = "ProductTypeID")
    public String ProductTypeName = "";
    /**
     * 产品类型编码
     */
    @DBCondition(CompareType = DBCondition.DBCompareType.like)
    @DBJoin(name = "Code", tableName = "fpc_producttype", tableAsName = "p1", Schemas = DBSchemas.PDM,
            MainTableField = "ProductTypeID")
    public String ProductTypeCode = "";

    /**
     * 规则字符串  暂时仅支持前缀固定 生成后续
     */
    @DBField
    public String RuleString = "";

    /**
     * 序列号长度
     */
    @DBField
    public int SNLength = 7;


    /**
     * 最后一次创建编码  序列化只与时间相关 与其他无关
     */
    @DBField
    public String LastBarCode = "";


    /**
     * 规则明细  一个规则明细内部只能包含一个序列号
     */
    @DBField(DefaultValue = "'[]'", CheckValue = true,isJson = true)
    public List<BCMRuleItem> RuleItemList = new ArrayList<>();


    public void CodeRuleString(String wOrderNo, String wProductNo, String wProductSimplifiedCode, String wFactoryCode) {
        if (RuleItemList == null || RuleItemList.size() == 0)
            return;

        List<BCMRuleItem> wBCMRuleItemList = new ArrayList<>();

        RuleItemList.sort(Comparator.comparingInt(o -> o.SequenceNo));
        RuleString = "";
        boolean isUsed = false;
        for (BCMRuleItem wBCMRuleItem : RuleItemList) {
            if (wBCMRuleItem == null)
                continue;

            if (wBCMRuleItem.RuleItemType == BCMRuleItemTypes.Sequence.getValue()) {
                if (isUsed)
                    continue;
                isUsed = true;
            }

            RuleString += wBCMRuleItem.getRuleCodeString(wOrderNo, wProductNo,wProductSimplifiedCode, wFactoryCode);

            wBCMRuleItemList.add(wBCMRuleItem);
        }
        RuleItemList.clear();
        RuleItemList.addAll(wBCMRuleItemList);
    }


    @JsonProperty("ComponentType")
    public int getComponentType() {
        return ComponentType;
    }

    public void setComponentType(int componentType) {
        ComponentType = componentType;
    }

    @JsonProperty("ComponentTypeText")
    public String getComponentTypeText() {
        ComponentTypeText = PDMComponentTypes.getEnumType(ComponentType).getLabel();
        return ComponentTypeText;
    }

    public void setComponentTypeText(String componentTypeText) {
        ComponentTypeText = componentTypeText;
    }

    @JsonProperty("RuleString")
    public String getRuleString() {
        return RuleString;
    }

    public void setRuleString(String ruleString) {
        RuleString = ruleString;
    }

    @JsonProperty("SNLength")
    public int getSNLength() {
        return SNLength;
    }

    public void setSNLength(int SNLength) {
        this.SNLength = SNLength;
    }

    @JsonProperty("LastBarCode")
    public String getLastBarCode() {
        return LastBarCode;
    }

    public void setLastBarCode(String lastBarCode) {
        LastBarCode = lastBarCode;
    }

    @JsonProperty("ProductTypeID")
    public int getProductTypeID() {
        return ProductTypeID;
    }

    public void setProductTypeID(int productTypeID) {
        ProductTypeID = productTypeID;
    }

    @JsonProperty("ProductTypeName")
    public String getProductTypeName() {
        return ProductTypeName;
    }

    public void setProductTypeName(String productTypeName) {
        ProductTypeName = productTypeName;
    }

    @JsonProperty("ProductTypeCode")
    public String getProductTypeCode() {
        return ProductTypeCode;
    }

    public void setProductTypeCode(String productTypeCode) {
        ProductTypeCode = productTypeCode;
    }

    @JsonProperty("RuleItemList")
    public List<BCMRuleItem> getRuleItemList() {
        return RuleItemList;
    }

    public void setRuleItemList(List<BCMRuleItem> ruleItemList) {
        RuleItemList = ruleItemList;
    }
}
